#include <GL/glut.h>
#include <GL/gl.h> 
#include <iostream>
#include <math.h>
#define FRAMES_PER_SECOND 100

using namespace std;

static int win=0;
int r=0;
void render(){
	// do  a clearscreen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode( GL_PROJECTION );
	glColor3f(1,0,0);
	glLoadIdentity();
	glRotatef(r,1,0,0);
	glOrtho( 0, 800, 600, 0, -1, 1 );
	
	cout<<r<<endl;
	glBegin(GL_TRIANGLES); 
		glColor3f(1,0,0);      
		glVertex3f(400,100,0); 
		glColor3f(0,1,0);      
		glVertex3f(750,500,0); 
		glColor3f(0,0,1);      
		glVertex3f(50,500,0);  
	glEnd(); 

	r++;
	if(r>360)
		r=0;
	
	glutSwapBuffers();
}	
void keyboard(unsigned char key, int x, int y){
	cout << "Pressed key " << key << " on coordinates (" << x << "," << y << ")";
	cout << endl;
	if(key == 'q'){
		cout << "Got q,so quitting " << endl;
	glutDestroyWindow(win);
	exit(0);
	}
}	
void timer(int value) {
    	
	glutTimerFunc(1000 / FRAMES_PER_SECOND , timer, 0);
	//glutTimerFunc(1000 / 60 , timer, 0);
    glutPostRedisplay();
}
int main(int argc, char **argv){
	std::cout<<"initialize glut window"<<std::endl;	
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
	// window size
	glutInitWindowSize(800,600);
	// window position
	glutInitWindowPosition(100,100);
	
	
	win= glutCreateWindow("first glut window"); // create window
	// set display function	
	glutDisplayFunc(render);
	
	// keyboard callback fnct
	glutKeyboardFunc(keyboard);
	
	
	// define the color we use to clearscreen 
	glClearColor(0.0,0.0,0.0,0.0);
	
	glutTimerFunc(1000 / FRAMES_PER_SECOND, timer, 0); //FPS Timer for auto Redraw
	//glutTimerFunc(1000 / 60, timer, 0); //FPS Timer for auto Redraw
   
	glutMainLoop(); // start the main loop
	
	
	
	return 0;
  
	
	
}
